//
//  Animal.m
//  刷新动画
//
//  Created by QQn1993 on 16/1/12.
//  Copyright © 2016年 QQn1993. All rights reserved.
//

#import "Animal.h"

@interface Animal ()

@property (nonatomic, strong) UIView *downLoad;
//box
@property (nonatomic, strong) UIImageView *box;
//heart
@property (nonatomic, strong) UIImageView *heart;
//boxarray
@property (nonatomic, strong) NSMutableArray *boxArray;

@end

@implementation Animal

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.box = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.box.image = [UIImage imageNamed:@"box_01.png"];
        
        [self addSubview:self.box];
        
        self.heart = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.heart.image = [UIImage imageNamed:@"heart.png"];
        self.heart.alpha = 0;
        [self addSubview:self.heart];
        
        self.boxArray = [[NSMutableArray alloc] initWithCapacity:0];
        for (int i = 1; i < 6; i++) {
            NSString *str = [[NSString alloc] initWithFormat:@"box_%02d.png", i];
            UIImage *image = [UIImage imageNamed:str];
            [self.boxArray addObject:image];
        }
        for (int i = 0; i < 30; i++) {
            UIImage *image = [UIImage imageNamed:@"box_05.png"];
            [self.boxArray addObject:image];
        }
        for (int i = 4; i > 0; i--) {
            NSString *str = [[NSString alloc] initWithFormat:@"box_%02d.png", i];
            UIImage *image = [UIImage imageNamed:str];
            [self.boxArray addObject:image];
        }
        
        
    }
    return self;
}

- (void)setContetsize:(CGSize)contetsize
{
    _contetsize = contetsize;
    
    self.box.frame = CGRectMake((contetsize.width -contetsize.width / 10) / 2, contetsize.height - contetsize.width / 10 / 1.23, contetsize.width / 10, contetsize.width / 10 / 1.23);
    
//    self.box.backgroundColor = [UIColor cyanColor];
    
    self.heart.frame = CGRectMake((contetsize.width - self.box.frame.size.width / 4 * 2) / 2, contetsize.height - contetsize.width / 10 / 1.23, self.box.frame.size.width / 4 * 2, self.box.frame.size.height / 4 * 3);
    
}

- (void)strat
{
    [self.box setAnimationImages:self.boxArray];
    [self.box setAnimationDuration:1];
    [self.box setAnimationRepeatCount:1];
    [self.box startAnimating];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 animations:^{
            [UIView setAnimationDelay:0];
            self.heart.frame = CGRectMake((self.frame.size.width - self.box.frame.size.width / 4 * 2) / 2, (self.contetsize.height - self.width / 10 / 1.23 - self.box.height / 4 * 3), self.box.frame.size.width / 4 * 2, self.box.frame.size.height / 4 * 3);
            self.heart.alpha = 1;
            
        } completion:^(BOOL finished) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.heart.alpha = 0;
                self.heart.frame = CGRectMake((self.frame.size.width - self.box.frame.size.width / 4 * 2) / 2, 0 - self.frame.size.height - self.frame.size.width / 10 / 1.23, self.box.frame.size.width / 4 * 2, self.box.frame.size.height / 4 * 3);

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                    [[NSNotificationCenter defaultCenter] postNotificationName:@"tag" object:[NSString stringWithFormat:@"滚回来"]];
                    
                });
            });
        }];
    });
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
